﻿@namespace MudBlazor.Docs.Examples

<MudGrid>
    <MudItem xs="12">
        <MudSlider Min="1" Max="20" Immediate="true" Step="1" @bind-Value="papers"></MudSlider>
    </MudItem>
    <MudItem xs="12">
        <MudSelect Label="Justify" T="Justify" @bind-Value="justification">
            <MudSelectItem Value="Justify.FlexStart">Flex Start</MudSelectItem>
            <MudSelectItem Value="Justify.FlexEnd">Flex End</MudSelectItem>
            <MudSelectItem Value="Justify.Center">Center</MudSelectItem>
            <MudSelectItem Value="Justify.SpaceAround">Space Around</MudSelectItem>
            <MudSelectItem Value="Justify.SpaceBetween">Space Between</MudSelectItem>
            <MudSelectItem Value="Justify.SpaceEvenly">Space Evenly</MudSelectItem>
        </MudSelect>
    </MudItem>
    <MudItem xs="12">
        <MudGrid Justify="justification">
            @for (int i = 0; i < papers; i++)
            {
                int localindex = i;
                
                <MudItem xs="@breaks[localindex]">
                    <MudPaper Class="d-flex flex-column align-center justify-center mud-width-full py-8">
                        <MudIconButton Icon="@Icons.Material.Filled.KeyboardArrowUp" OnClick="(()=>UpdateBreaks(localindex,1))" Size="Size.Small" />
                        <MudText Typo="Typo.h6" Align="Align.Center">@breaks[localindex]</MudText>
                        <MudIconButton Icon="@Icons.Material.Filled.KeyboardArrowDown" OnClick="(()=>UpdateBreaks(localindex,-1))" Size="Size.Small" />
                    </MudPaper>
                </MudItem>
             }
        </MudGrid>
    </MudItem>
</MudGrid>

@code{

    int _papers = 7;
    int papers { get { return _papers; } set { UpdatePapers(value); } }

    int[] breaks = new int[7] { 3, 4, 2, 3, 3, 2, 4 };

    Justify justification = Justify.FlexStart;

    void UpdatePapers(int value)
    {

        int[] newbreaks = Enumerable.Repeat(3, value).ToArray();

        for (int i = 0; i < newbreaks.Length; i++)
        {
            if ( i < breaks.Length)
            {
                newbreaks[i] = breaks[i];
            }
        }

        breaks = newbreaks;

        _papers = value;

        StateHasChanged();
    }

    void UpdateBreaks(int index,int changeamount)
    {
        List<int> newbreaks = breaks.ToList();
        if (newbreaks[index] + changeamount > 12 || newbreaks[index] + changeamount < 1) return; //don't go above 12 or below 1
        newbreaks[index] += changeamount;

        breaks = newbreaks.ToArray();

        StateHasChanged();
    }
}
